I've encountered this underlined phrase in the below context,

e.g. They construct grammar. In so doing they make generalizations, they test those generalizations or hypothesis.

At the first time I saw that phrase, I felt that it should be 'in doing so' for the reason that 'do so' is the basic form of subsituting for the verb phrase.
And then, I came to think that it might be the inversion occured to bring some particular emphasis on the connection between sentences.

Leaving my conjecture aside, I'm really wondering why this phrase
is grammatically correct. What do you think about it?

'In so doing' is fine. It adds a bit of elegance and style to the sentence.
Don't use it for mundane things.
OK. He betrayed Mary's trust. In so doing, he broke her heart.

not OK. He took out the garbage. In so doing, he dropped some.

Thank you for the answer.
Almost nowhere else could I find the difference.
So the information in your answer is very valuable. Thanks again.