M.S.
This think this is normal developmental behavior at his age. My son is 3 yrs too and does the same thing. I can imagine your embarressment and shock. I don't think he is doing it for attention but I think that your reaction does affect how he feels about his body. I tell my son that that is something you do in "private" and I try to say it in a nonemotional way. I have never had to explain not doing it in public but I would handle it the same way if I had to.
I am a pediatric nurse and a an early childhood educator but nothing prepares you for caring for children more than parenthood itself!