The original phrase was "in the name of God". The idea was that the speaker must tell the truth. They must swear an oath.
So:
Judge: Tell me, did you steal the bread?
Accused: In the name of God, I swear that I did not steal the bread.
or
Lord of the manor: In the name of God man, What are you doing at my door?
Interloper: My lord I am only a poor peasant, trying to beg a crumb. This I swear.
EDIT
As Sarriesfan points out in a comment. "in the name of Merlin" is an invention of the author, J.K. Rowling. It is not an idiom. She uses the name of a famous wizard from folklore as a euphemism for 'God'.