Naturally, a soul would be fundamentally connected to the body
No. The soul being distinct from the body is usually very much the point.
Therefore, the only explanation
No. Even if we exhausted all the many, many metaphysical models already postulated, there could be any number of unknown and indeed even inconceivable explanations, or no reason at all. Especially since The Supernatural is the premise here.
some kind of malicious otherworldly entity.
Why assume it to be malicious? It could merely be, say, imperfect, or capricious, or acting in inscrutable mysterious ways towards some cosmic goal beyond mortal comprehension (and you, of all people, are not in a position to protest that the Greater Good™ does not justify the means!)…
And again, the active actions of an intelligent being are a big assumption. To give just a few examples, conceivably, souls could mutate, they could have to find their own way to a body and thus go wrong by themselves, they could form through some fallible metaphysical process parallel to material pregnancy…
Can we, for instance, travel to the plane of existence in which this entity resides, and use magic runes to destroy or at least imprison it so it cannot continue to switch souls into the wrong bodies?
Jumping right to deicide… a bit hubristic, aren’t we?
To go back to the Cosmic Greater Good: Even if the entity could be slain, that could have FAR more dire consequences than removing the slight inconvenience of a tiny portion of people getting souls of the wrong gender - certainly a small price conquered to what plenty of other beings essential to the continued existence of the world in myth and fiction require.
If fighting the entity responsible is not possible, would it be possible to build a machine to align souls with the bodies? Most people did not think souls were sexual, and that it was the body that was sexual, with the soul residing in it, but if the soul is sexual, is there a way to change the sex of the soul, rather than mutilating the body? If I believed in this theory, I would also want to explore the possibility of soul-switching: can you take a boy who has the wrong soul and a girl who has the wrong soul and switch their souls into the other’s body?
It is an extreme metaphysical claim that a soul can be in the wrong body, and yet somehow, this is not being addressed at all. They just declare it, without elaborating on the mechanism. If people believe in this sort of thing, there is no reason we shouldn’t be looking at using magic to rectify the situation. If someone believes a soul can be switched in the first place, there is no reason it cannot be switched back, which would prevent so many problems.
…so, is this an attempt to find a new edge by rebranding to a mad scientist dabbling with psychic and occult forces Man was not meant to know?