It all depends on what you mean by "scratch." If you already have a bike and are simply looking to upgrade it with new parts, then it is cheaper to buy a complete mountain bike. However, if you're starting from zero and need to purchase both a frame and components, then it's generally cheaper to build your own mountain bike.
The main cost associated with building a mountain bike from scratch is the price of the frame. A high-quality mountain bike frame can cost upwards of $1,000. But once you have the frame, the rest of the process becomes much more affordable. You can typically find quality components for under $500, and when put together correctly, these parts will last just as long as their more expensive counterparts.
Of course, there is also the issue of time investment. It takes significantly longer to assemble a mountain bike from scratch than it does to simply buy one off the shelf. But if you enjoy working on bikes and are patient enough to do your research before making any purchases, then building your own mountain bike can be a very rewarding experience.