In this case I think it makes sense to borrow the 40K for 5 years in order to get the mortgage down to 5% as opposed to 6%.
I would do it.
From payment 1 she is paying less interest.
From payment 1 her principal paydown is accelerated. She will owe less overall at any point in the future because she is paying down the 40k over a shorter time frame.
The downside is that she may have to pay the 401k loan is she switches jobs or pay a penalty and taxes. Although some 401k programs allow ex-employees to continue paying the loan as long as they hold their funds in the 401k after leaving the eomployer.