Here is a link about this topic for your reference: Limit who users can see when searching the directory in Teams
You may consider either using Address Book Policy (Exchange Online) or Information Barriers (require license) to achieve this.
Also note that: If using either option, you must turn on search by name in the Teams admin center, which may take a few hours to take effect.
How to turn on search by name:
- In the Microsoft Teams admin center, select Teams > Teams settings.
- Under Search by name, next to Scope directory search using an Exchange address book policy, turn the toggle On.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.