Getting a working instance of the command \verb into \href's 2nd argument can be done by having stuff that goes into \href's 2nd argument read from .tex-input-file and tokenized under verbatim-category-code-régime and "then fed" to \scantokens under usual category-code-régime for re-tokenization as usual.
For retokenizing under usual category-code-régime you might like to have a % at the end of the line which then gets interpreted as a comment-character that prevents \scantokens' last inserted endline-character from being effective.
Instead of \tex the command is called \MyMacro in the following.
\MyMacro relies on its arguments being read and tokenized while verbatim-catcode-régime is in effect.
Therefore \MyMacro works out
- when obtaining the tokens that form these arguments by reading and tokenizing from .tex-input-file.
- when the tokens forming these arguments at some other moment were read and tokenized while verbatim-category-code-régime was in effect before passing them on to
\MyMacro.
Therefore \MyMacro in many situations does not work out if the tokens forming its argument were read and tokenized under a category-code-régime differing from verbatim-category-code-régime, e.g., under usual category-code-régime.
E.g., \section{\MyMacro{\verb|stackexchange.com $1|}{test}} does not work out because the tokens forming the arguments of \MyMacro were tokenized under usual category-code-régime (instead of verbatim-category-code-régime) when the argument of \section was read from the .tex-file and tokenized.
Usage of \MyMacro within moving arguments or pdf-strings is generally a big problem.
(A "moving argument" in TeX-jargon is a macro argument which can wind up automatically in many places in the document. E.g., arguments of sectioning-commands like \chapter and \section are moving arguments because they can make it into the table of contents, into \nameref-cross-references, into page-headers, into pdf-bookmarks,...
A pdf-string, e.g., the text of a bookmark within the .pdf-file, is a component of the "infrastructure" of the .pdf-file itself and needs to be encoded in a way which complies to pdf-standards and thus can be understood by programs for viewing and/or otherwise processing .pdf-files.)
\documentclass{article}
\usepackage{hyperref}
\begingroup
\catcode`\Z=14 %
\catcode`\%=12 Z
\csname @firstofone\endcsname{Z
\endgroup
\newcommand\scantokenswithoutendline[1]{\begingroup\newlinechar=-1 \escapechar=92 \scantokens{\endgroup#1%}}Z
}%
\NewDocumentCommand{\MyMacro}{vv}{%
\href{https://tex.stackexchange.com/#2}{\scantokenswithoutendline{#1}}%
}%
\begin{document}
% This creates a link to the not-existing page
% https://tex.stackexchange.com/test .
% The phrase of the link is: \verb|stackexchange.com $1|
\MyMacro{\verb|stackexchange.com $1|}{test}
\end{document}

\verbhrefcommand that absorbs the second argument verbatim, but then you need to escape special characters in the URL. The\hrefcommand works hard to be able to absorb the first argument without the need to escape special characters. Do you expect such URLs? – egreg Sep 04 '22 at 16:08