Simply a lower-cost (read "free") alternative to hiring more air marshals.
Armed pilots outnumber air marshals now in the states. Costs the government next to nothing. Provides somewhat of a deterrent. No problems so far.
Yes, it would be tricky to defend against determined hijackers. Only thing worse would be doing it without a weapon....