It is something you do subconsciously; it has nothing to do with what you are thinking about. There is a myth in Greek that a certain God wanted to be very knowledgeable so he went to a wizard who gave him some sort of potion to rub on his head. Apparently, that's how we started scratching our heads; to get knowledge.
I think that when you scratch your head, you are showing just that your thinking or maybe it's just easier to think like when you do homework its usually easier to think when you listen to music with no words. :D
this is just an opinion. But i think that when in deep thought the nerves in your head are overly stimulated causing you to scratch your head. Over active nerves could be as a result of hyper active neurons in the brain itself. Just a thought. I have no scientific backing to support this hypothesis.