A short question that will probably never get you a short answer from a nonprofit lawyer. The IRS calls this "unrelated business income," and the rules about UBI can be pretty complex. Nonprofit lawyers and tax-exempt CPAs spend a lot of time trying to learn and navigate those rules.
Let's start with the heart of your question: can a nonprofit sell items at a markup and use the proceeds to advance the mission? Generally, yes. It is not prohibited or illegal for a nonprofit to do that. But, that general "yes" doesn't mean that there won't be consequences, and a complete answer to the question of consequences involves looking at the type of nonprofit, what items it's selling, how frequently, where it gets the items, how much of its revenue comes from the sales, and how the sales are related to its "exempt purpose."
First, what's the type of nonprofit? There are different UBI rules for rules for organizations that are covered by 501(c)(7), 501(c)(9), 501(c)(17). But let's use your example of a pet rescue that sells pet supplies. They are probably a 501(c)(3), so the "normal" UBI rules apply, which involve answering the next three questions.
Second, is this a "trade or business?" The IRS says that's "any activity carried on for the production of income from selling goods or performing services." So, in your example of selling bandanas and treats, it's a trade or business.
Third, is this trade or business "regularly carried on?" This means the IRS wants to know if they are done frequently or continuously, making them similar to (and competing with) for-profit businesses.
Fourth, is the income "not substantially related to furthering the exempt purpose of the organization?" It does not matter if the money from the sales is used to further the exempt purpose. What the IRS wants to know is if the sales themselves are part of the mission. In your pet rescue example, the sales of bandanas and treats are not substantially related to the exempt purpose. Here's some other examples: a museum gift shop sells educational books about the subject matter of the museum. Those sales are "substantially related" to the mission of the museum, and they are not UBI. On the shelf next to the books, the same gift shop sells a stuffed animal for children patrons. Those are likely not substantially related, and it's UBI.
If it's a "trade or business" that is "regularly carried on" and "not substantially related," then it's UBI.
But, finally, there are a bunch of exceptions and carve outs. A big one that would (possibly) apply to your animal rescue example is that the IRS does not consider it UBI for a charity to sell items that were donated to it. So if a company or generous donor gave the bandanas and treats to the nonprofit, it's not UBI to sell them at a mark-up.
So, if it's not illegal to sell the items, why are there all these questions? It's because a nonprofit has to pay taxes on its unrelated business income. And, in some cases, if it starts to look like the for-profit "unrelated" business is starting to drown out the non-profit purposes, the IRS might revoke the tax-exempt status of the nonprofit.