well.. it's been crying for help for a while now and you've been ignoring it.. why now are you concerned with fixing it???
it poping out of gear was a SIGN. it either had a bad pto bearing, a loose detent ( not likely ).. or most likely a damaged split ring or bearing carrier. you will need to pull the shaft and the pto shifter cover possibly, to find out more.
remember the pto shaft passes thru the hyds sump.. so you must drain the hyds and diffy before pulling the shaft.
what happens is the shaft can move in and out and walk out of engagement, damaging the splines on the end of the shaft and the sliding coupler.. it can also mushroom the end of the shaft. when one or both of these happens.. well.. she becomes hard to or won't engage.