When this passage came up in my research last year, I found the explanation by Malamat (1950, 'The Last Wars of the Kingdom of Judah', in Journal of Near Eastern Studies 9, 218–227) the most compelling (p. 219):
The first discovery that was a turning point in understanding the chain of events in the last years of King Josiah's reign was the Babylonian Chronicle (describing the years 616–609), published by Gadd in 1923,7 which explains why Josiah attempted a stand at Megiddo against the powerful Egyptian army. The Battle of Megiddo is no longer a reckless adventurous step taken by Josiah, as was usually described by historians, but rather a bold decision based on far-reaching political and military considerations. As is known, the Babylonian Chronicle proves that Assyria and Egypt, which until now had been archenemies, concluded a military mutual aid pact against Babylon and Media, which were overrunning Assyrian cities. In 610, a year before the Battle of Megiddo, the Babylonian army, with the help of the Umman-Manda, conquered the city Ḥaran and established control of the whole region as far west as the Euphrates. This constituted a threat to Syria. Egypt therefore sent aid to her Assyrian ally, the remnants of whose army were quartered in Syria. It is clear that Josiah's attempt to detain the Egyptian army at Megiddo came to prevent that Egyptian military assistance.8 Josiah's action allies Judah with Babylon; perhaps it even signifies a phase of a broad strategic plan, a military alliance between Babylon and Judah.9
7. Gadd, The Fall of Nineveh (1923).
8. Cf. II Chron. 35:21. The concept בית מלחמתי in this passage has not been satisfactorily explained. According to one opinion, it refers to the permanent encampment of Pharaoh in Syria or to his front line (cf. J. Lewy, "Forschungen zur alten Geschichte Vorderasiens," MVAG , p. 21). Another opinion has it that it refers to Riblah, Pharaoh's headquarters in Syria (cf. B. Alfrink, "Die Schlacht bei Megiddo und der Tod Josias," Biblica, 1934, who translates: "Kriegsstadt, Garnisonsstadt, Festungsstadt").
9. The biblical story pertaining to Chezkiah's reign about Merodach Bal'adan testifles to a much earlier contact between representatives of Judah and Babylon (II Kings 20:12 ff.).