If a player grabs a piece to move it, and before placing it in its new position, the player gets second thoughts about the best position for the piece, and so they decide to think a little bit more. While they are thinking and holding the piece, they get an urge to visit the bathroom. In that case, is it legal for the player to take the piece to the bathroom until they are back to resume the game? This assumes that the player will never let go of the piece throughout the entire process until it's placed in its new position.
There are a number of serious things wrong with what you describe:
While they are thinking and holding the piece
You may hold a captured piece while you think but you may not hold a piece that is part of the current position. Your opponent has a right to a full view of the current position on the board at all times except the second or two it takes you to move. Do this in a tournament and be prepared to be punished. At a minimum expect a warning and likely a time penalty.
In that case, is it legal for the player to take the piece to the bathroom until they are back to resume the game?
No! Do this and you risk forfeiting the game.
From the FIDE Laws of Chess:
11.2.1 The ‘playing venue’ is defined as the ‘playing area’, rest rooms, toilets, refreshment area, area set aside for smoking and other places as designated by the arbiter.
11.2.2 The playing area is defined as the place where the games of a competition are played.
11.2.3 Only with the permission of the arbiter can:
184.108.40.206 a player leave the playing venue,
220.127.116.11 the player having the move be allowed to leave the playing area.
In general the toilets are part of the playing venue but not part of the playing area. If you get up from the board while it is your turn you may move about the playing area (and, for instance, look at other boards) but you may not leave the playing area (go to the toilet, go for a smoke or vape, get a drink, food, etc.) without getting the arbiter's permission. If you do so you will likely forfeit the game for possible cheating.
From FIDE's arbiter manual: "11.5 It is forbidden to distract or annoy the opponent in any manner whatsoever."
I think picking up a piece and keeping it out of the board for a long time is quite disturbing. As far as I know there's no problem on picking up a piece and placing it back on its original square as long as it's the piece you finally move. Probably some sort of gesture or verbal comment would be advisable, but that would be it.
A tangential point related to what I think is a misconception behind the question:
This assumes that the player will never let go of the piece throughout the entire process until it's placed in its new position.
It looks like the motivation for the question is the idea that once you grab a piece you can't let go of it until you make your move. What the FIDE rules actually say is (emphasis added):
4.7 When, as a legal move or part of a legal move, a piece has been released on a square, it cannot be moved to another square on this move.[...]
One might think that releasing the piece back in its original square might constitute an illegal move. But note this other rule:
7.5.1 An illegal move is completed once the player has pressed his clock.[...]
So in principle you could release the piece in as many illegal places as you like as long as you don't press the clock before releasing the piece in a legal square. But since there's also the issue of annoying the opponent, which has been brought up in other answers, I would say that least annoying option if you want to let go of the piece before completing your move is to put it back in its original square.
converting my comment to answer:
why not just put the piece aside or put the piece back in the original position? Giri did the latter against Anand but personally i do find it a little weird even if it is technically within the rules. i think i'd go with putting the piece on the side of the board to indicate to both players 'yeah i touched this piece but i have to go. BRB' w/c is your intention anyway right?