It is really interesting to read Ezra and see the Hitler like racism promoted by this Biblical prophet. For the men of Israel have married women from these people and have taken them as wives for their sons. So the holy race has become polluted by these mixed marriages.
This is a common taking things out of context. Foreigners could convert to Judaism, but very few of them did because it wasn't easy. These men were marrying foreign women. The pollution was not genetic but religious and cultural. These foreign women's values would pass on to their children. You can read for yourself in Numbers 12 what happened when Miriam and Aaron questioned Moses, and by so doing God also, about his marrying a foreign (probably darker skinned African) wife. What Ezra is talking about has to do with the prohibition against marrying foreigners in Deuteronomy 7:3. You appear to not have known that, otherwise you would have mentioned it.
Ezra was not a Biblical Prophet to begin with. He was a Priest who when bringing the people of Judah back from captivity created the Jewish government run by the Great Sanhedrin. Ezra was the one who established this Sanhedrin which eventually limited the authority of the Prophets to just "merely messengers" from God. It was the Sanhedrin that were to interpret what the Prophets said giving them ability to twist the words of God to fit their narratives. As far as the people keeping their nation pure from outsiders, that's pretty much most civilizations of the past. Jews have always tried to keep their Jewishness as pure as possible. And, some of the reasons come from the Old Testament. There were times God did tell the people to not marry certain groups of wicked people. Mostly because of the wicked traditions of those people. Idolatry being a major reason.
You should read that crap attributed to him in 1 & 2 Esdras = https://classic.biblegateway.com/versions/Common-English-Bible-CEB/#booklist