I'll be happy to edit when you provide answers to the question I posed in the comments.
Given the choice (and I assume there is no other) I'd take a loan from the 401(k) vs a withdrawal.
You withdraw $40K. I'll assume 25% bracket as you're planning at least a $200K house. Hopefully, your taxable income is above $38K, the 25% line for singles. The tax and penalty is 35% total, federal. You net $26K. And you have $40K less in the retirement account. In 40 years, at 10% average growth, that's $1.8M you won't have in your 401(k). And as littleadv stated, no deposits for 6 months, meaning no matching. There's a few more thousand you'll lose.
You borrow $20K. Your 401(k) will see a return on the $20k that's better than the short bond account, 4-5% vs less than 1%. You are short $6K, but in return have paid no tax, no penalty, etc. I respect those who are strongly anti-loan, but even they would agree, this is the far lesser of 2 evils.
The above is pretty generic, there are better choices. But your CPA friend's advice is nearly as bad as it gets. By the way, the tax you'll save once you have the mortgage has nothing to do with that 10% penalty. Say you bought the house with cash (as many would be happy to do). You'd pay the penalty for the 401(k) withdrawal, but have no mortgage deduction. If you had the 20%, you still have a loan and the deduction, but no penalty for taking his bad advice.
My advice is to take that refund and use it to pay the loan faster.