if it's lightly, then i think its ok.
but the thing is, dont let it become a habit.
when he/she is doing it, stop him/her n tell him/her firmly, NO.
alternatively, my cousin did this to her head banging daughter.
you know those headband mummies like to put on baby head. get those wider ones.
cut open the side n slip a sponge into it. then sew it up.
let baby wear it, so even when he/she bang also can be cushioned.
but of cos, it is only a temp solution. best to stop baby from banging head.