A.S.
My son (now 30 months) did this until he was 18 months and then grew out of it. When I spoke to my doctor about it (since I was very concerned as well), he said that he would not hit his head hard enough to actually harm himself. I always just tried to stay calm and talk to him since it seemed like when I tried to stop him, it made him do it more.
Good luck and hang in there, it most likely will pass.