Using the shoulder to pass someone is usually not legal.
However if you get into the lane to pass someone and cannot pass that person or vehicle in time before another vehicle is coming head on at you then get into that shoulder lane to get out of the way of the vehicle and then once that vehicle has passed then merge back into the lane and get over into the correct lane as soon as safely possible.
Although we're not supposed to legally drive on the shoulder I will sometimes pull over to the shoulder and continue driving to let a Semi or other vehicle get around me if I'm driving a bit slower.
That way they don't have to get into another lane to pass me and possibly cause a head on crash.
It's safer for me to pull into the shoulder lane and let them by than have them go into the other lane and risk a head on collision.