How to assign arbitrary LaTeX text to a variable for use in different places in a document?

Keep the tests out of your document body:

\documentclass[12pt]{book}% 
\usepackage{amsmath}

\ifdefined\HCode
\newenvironment{myequation}
{\begin{equation}}{\end{equation}\ignorespacesafterend}
\else
\usepackage{breqn}
\newenvironment{myequation}
{\begin{dmath*}}{\end{dmath*}\ignorespacesafterend}
\fi

\begin{document}    


\begin{myequation}
  3\frac{d^{2}y}{dt^{2}}+2\frac{dy}{dt}+y\left(  t\right) = u(t)
\end{myequation}

\end{document} 

Not exactly as you suggested, but \newif is a simple solution. Your A and B environments replaced by exemplary theorem-like ones.

Edit: enlarged, according to suggestion from comment. The old picture left.

   \documentclass{article}

\usepackage{amsmath}

\begin{document}
\def\myVariable{  whatever I want goes here } %tell Latex not to look at it

Now I can write

\newif\ifHcode

\newtheorem{thm}{Theorem}
\newtheorem{lem}{Lemma}

%\ifdefined\HCode
\def\usemyVariable{
\ifHcode
  \begin{thm}        
       \myVariable
  \end{thm}
\else
  \begin{lem}
      \myVariable
  \end{lem}
\fi
}

\usemyVariable

\Hcodetrue

\usemyVariable

\def\myVariable{3\frac{d^{2}y}{dt^{2}}+2\frac{dy}{dt}+y\left( t\right) = u(t)}

\def\usemyVariable{
\ifHcode
\begin{equation}
  \myVariable
\end{equation}
\else
\begin{displaymath}
  \myVariable
\end{displaymath}
\fi 
}

\usemyVariable

\Hcodefalse

\usemyVariable



\end{document}

enter image description here