Sure. Sometimes it's a horrible idea, and sometimes it's the best move you've got.
In the general sort of position pictured above, White has other options for relieving the pin, of course.
Something like c2-c3 (to stop Black from playing Nd4 and ganging up on the pinned Knight) followed by Re1 (to clear the f1-square), then N(b1)-d2-f1-g3 kicking the Bishop away.
This position has come up so many times it is not even funny.
My question is, in the above scenario is it ever ok to move to g4? Or in similar scenarios? I watched a grandmaster who said he told his students to never under any circumstance to do that move, but I sometimes do it in game and stockfish marks it as excellent.