How can line code polarity inversion happen?
how can such a sudden change in polarity occur in practice? 


In a wireless/bandpass channel, that'd be a 180° phase shift.

I've never seen that occur anywhere on a wired (baseband signaling) link that you'd use NRZ-whatever on, and aside from "blatant bugs in the hardware design", I wouldn't see how this would occur in practice.


  and is it really a problem?


Not that I can think of, no.

Even if we came up with a model where we get polarity reversals, they'd be "worst case" errors (our whole line code being designed the fact that the two levels are as strongly not the other as possible, you'd need the maximum physical error to achieve that) – and hence rare (otherwise our line code choice would have been bad, and we should have used a physical layer that is robust against these).

Rare errors on links are typically countered either by checksums and re-requesting broken frames, or by forward error correction. Of the latter, you can definitely devise a scheme that converts a continuous bit flip starting at specific position to a single bit error, which might be well-recoverable with the given code. However, this would have little to do with the line coding.